<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import { fetchList, fetchPv } from '@/api/ApiService'
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null,
      data_legend: ['Industries', 'Technology', 'Forex', 'Gold', 'Forecasts'],
      data_series:[
              { value: 320, name: 'Industries' },
              { value: 240, name: 'Technology' },
              { value: 149, name: 'Forex' },
              { value: 100, name: 'Gold' },
              { value: 59, name: 'Forecasts' }
            ],
      listQuery: {
      //  page: 1,
      //  limit: 20,
      // importance: undefined,
      //  title: undefined,
      //  type: undefined,
      //  sort: '+id'
      }
    
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.getList()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    getList() {
      this.listLoading = true
      this.listQuery.ActionCode ='SearchStockPrepareInTimeReport';
      fetchList(this.listQuery).then(response => {
        this.list = response.data.items
        this.data_legend = []
        this.data_series = []
        var data = {};
        this.list.forEach((item,index,array)=>{
          if(data[item["物料代码"]]){
            data[item["物料代码"]] += item["数量"]
          }else{
            data[item["物料代码"]] = item["数量"]
          }           
        })

        var i = 0;
        for(var key in data){
           i++;
           if(i<10){
            this.data_legend.push(key);           
            this.data_series.push( { value: data[key], name: key });
           }
        }

        this.initChart()

        this.listLoading = false
      })
    },
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')

      this.chart.setOption({
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: {
          left: 'center',
          bottom: '10',
          data: this.data_legend
        },
        series: [
          {
            name: 'WEEKLY WRITE ARTICLES',
            type: 'pie',
            roseType: 'radius',
            radius: [15, 95],
            center: ['50%', '38%'],
            data: this.data_series,
            animationEasing: 'cubicInOut',
            animationDuration: 2600
          }
        ]
      })
    }
  }
}
</script>
